package com.luhan.fmmall.service.impl;

import com.luhan.fmmall.dao.UsersMapper;
import com.luhan.fmmall.entity.Users;
import com.luhan.fmmall.service.UserService;
import com.luhan.fmmall.utils.Base64Utils;
import com.luhan.fmmall.utils.MD5Utils;
import com.luhan.fmmall.vo.ResStatus;
import com.luhan.fmmall.vo.ResultVO;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;
    //用户注册
    @Override
    @Transactional
    public ResultVO userResgit(String username,String pwd){
        //判断用户是否已经注册
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);

        List<Users> users = usersMapper.selectByExample(example);

        //如果没有查询到用户，则可以注册
        if (users.size()==0){
            Users user=new Users();
            String md5Pwd = MD5Utils.md5(pwd);
            user.setUsername(username);
            user.setPassword(md5Pwd);
            user.setUserImg("img/default.png");
            user.setUserModtime(new Date());
            user.setUserRegtime(new Date());
            int i = usersMapper.insertUseGeneratedKeys(user);//回填主键id
            if (i>0){
                return new ResultVO(ResStatus.OK,"注册成功！",user);
            }else {
                return new ResultVO(ResStatus.NO,"注册失败！",null);
            }
        }else {
            return new ResultVO(ResStatus.NO ,"用户名已经被注册!",null);
        }
    }

    //用户登录
    @Override
    public ResultVO checkLogin(String username, String pwd){
        //根据用户名查询出user对象
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        List<Users> users = usersMapper.selectByExample(example);
        if (users.size()==0){
            return new ResultVO(ResStatus.NO,"登录失败,用户名不存在!",null);
        }else {
            String md5Pwd = MD5Utils.md5(pwd);
            if (users.get(0).getPassword().equals(md5Pwd)){
                HashMap<String,Object> map = new HashMap<String,Object>();
                //如果登录成功，则需要生成令牌token(token就是按照特定的规则生成的字符串)
                //使用jwt规则生成token字符串
                JwtBuilder builder = Jwts.builder();
                String token = builder.setSubject(username) //主题，就是token中携带的数据
                        .setIssuedAt(new Date()) //设置token的⽣成时间
                        .setId(users.get(0).getUserId() + "") //设置⽤户id为token id
                        .setClaims(map) //map中可以存放⽤户的⻆⾊权限信息
                        .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000)) //设置过期时间
                        .signWith(SignatureAlgorithm.HS256, "laichunlan520") //设置加密⽅式和加密密码
                        .compact();
                return new ResultVO(ResStatus.OK,token,users.get(0));
            }else {
                return new ResultVO(ResStatus.NO,"登录失败，密码错误!",null);
            }
        }
    }
}
